package com.ruoyi.app.common

import cn.hutool.core.util.ReUtil
import cn.hutool.http.HttpUtil
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.ruoyi.app.domain.vo.VideoVo

fun String.location(): List<String> = HttpUtil.createGet(this).execute().headerList("Location")
fun String.num(regex: String): String = ReUtil.findAll(regex, this, 1)[0]
fun String.json(): JSONObject = JSON.parseObject(this)
fun String.jsonArray(): JSONArray = JSON.parseArray(this)


fun String.pipixia() =
    HttpUtil.get(
        "https://is.snssdk.com/bds/cell/detail/?cell_type=1&aid=1319&app_name=super&cell_id=${
            location()[0].num(
                "/item\\/(.*)\\?/"
            )
        }"
    )
        .json()
        .let {
            VideoVo().apply {
                title =
                    it.getString("data").json().getString("data").json().getString("item").json().getString("content")
                cover = kotlin.run {
                    val temp =
                        it.getString("data").json().getString("data").json().getString("item").json().getString("cover")
                            .json().getString("url_list").jsonArray()[0] as String
                    temp.json().getString("url")
                }
                url = kotlin.run {
                    val temp =
                        it.getString("data").json().getString("data").json().getString("item").json()
                            .getString("origin_video_download")
                            .json().getString("url_list").jsonArray()[0] as String
                    temp.json().getString("url")
                }
            }
        }

fun String.huoshan(): VideoVo =
    HttpUtil.get("https://share.huoshan.com/api/item/info?item_id=${location()[0].num("/item_id=(.*)&tag/")}")
        .json()
        .let {
            VideoVo().apply {
                cover = it.getString("data").json().getString("item_info").json().getString("cover")
                url = "https://api-hl.huoshan.com/hotsoon/item/video/_playback/?video_id=${
                    it.getString("data").json().getString("item_info").json().getString("url")
                        .num("/video_id=(.*)&line/")
                }"
            }
        }

fun String.douyin(): VideoVo =
    HttpUtil.get("https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id=${location()[0].num("/video\\/(.*)\\/?/")}")
        .json()
        .let {
            if (it.getIntValue("status_code") == 0) {
                VideoVo().apply {
                    title = it.getString("aweme_detail").json().getString("desc")
                    cover = it.getString("aweme_detail").json().getString("video").json().getString("origin_cover")
                        .json().getString("url_list").jsonArray()[0] as String
                    url = it.getString("aweme_detail").json().getString("video").json().getString("play_addr")
                        .json().getString("url_list").jsonArray()[0] as String
                }
            } else throw Exception("解析失败")
        }


fun String.weishi(): VideoVo =
    (if (indexOf("h5.weishi") != -1) HttpUtil.get(
        "https://h5.weishi.qq.com/webapp/json/weishi/WSH5GetPlayPage?feedid=${
            num(
                "/feed\\/(.*)\\b/"
            )
        }"
    )
    else HttpUtil.get("https://h5.weishi.qq.com/webapp/json/weishi/WSH5GetPlayPage?feedid=$this"))
        .json()
        .let {
            VideoVo().apply {
                title = kotlin.run {
                    val temp = it.getString("data").json().getString("feeds").jsonArray()[0] as String
                    temp.json().getString("feed_desc_withat")
                }
                cover = kotlin.run {
                    val temp = it.getString("data").json().getString("feeds").jsonArray()[0] as String
                    val temp1 = temp.json().getString("images").jsonArray()[0] as String
                    temp1.json().getString("url")
                }
                url = kotlin.run {
                    val temp = it.getString("data").json().getString("feeds").jsonArray()[0] as String
                    temp.json().getString("video_url")
                }
            }
        }

fun String.zuiyou() =
    HttpUtil.get(this).let {

        val video = it.num("/fullscreen=\\\"false\\\" src=\\\"(.*?)\\\"/")
        val videoTitle = it.num("/:<\\/span><h1>(.*?)<\\/h1><\\/div><div class=/")
        val videoCover = it.num("/poster=\\\"(.*?)\\\">/")
        val videoAuthor = it.num("/<span class=\\\"SharePostCard__name\\\">(.*?)<\\/span>/")
        VideoVo().apply {
            title = videoTitle
            cover = videoCover
            url = video.replace("u002F", "").replace("\\", "/")
        }
    }

fun String.kuaishou() {


}

fun String.xigua() {
}

fun String.huya() {

}


//fun String.spider(): VideoVo =
//    if (indexOf("pipix") != -1) {
//        pipixia()
//    }else if (indexOf("huoshan") != -1) {
//        huoshan()
//    }else if (indexOf("h5.weishi") != -1) {
//        weishi()
//    }else if (indexOf("douyin") != -1) {
//        douyin()
//    }else {
//        VideoVo()
//    }

fun String.spider(): VideoVo? =
    HttpUtil.post("https://tenapi.cn/v2/video", mapOf("url" to this))
        .json()
        .let { result ->
            result.takeIf { it.getIntValue("code") == 200 }
                ?.let {
                    it.getJSONObject("data").let {
                        VideoVo().apply {
                            cover = it.getString("cover")
                            title = it.getString("title")
                            url = it.getString("url")
                        }
                    }
                }
        }

fun main() {
    "https://v.douyin.com/Sj3egm5/".spider()
}
